import os, sys, tempfile, zipfile
import simplejson as json
from PyQt4.QtGui import * 
from PyQt4.QtCore import * 

from sprite import *
from spriteeditor import *
from map import *
from maptilelist import *
from maplayerlist import *

class MapEditor(QMainWindow):
    def __init__(self, width, height):
        QMainWindow.__init__(self, None)
        self.hbox = QHBoxLayout()

        self.map = Map(width, height, self)

        self.map_scrollarea = QScrollArea()
        self.map_scrollarea.setWidget(self.map)

        self.hbox.addWidget(self.map_scrollarea)

        top = self.geometry().y()

        self.maptilelist = MapTileList(self.map)

        self.maplayerlist = MapLayerList(self.map)

        self.hbox.addWidget(self.maptilelist)

        self.hbox.addWidget(self.maplayerlist)

        self.hbox_container = QWidget()
        self.hbox_container.setLayout(self.hbox)

        self.setCentralWidget(self.hbox_container)

        self.initialize_menu_bar()

        # file io

        self.savename = ""

    def add_menu(self, menu_name, actions, menu = None):
        if not menu:
            menu = self.menuBar().addMenu(self.tr(menu_name))

        for action in actions:
            if action[0] != "SEPARATOR":
                qa = QAction(self.tr(action[0]), self)
                qa.setShortcut(self.tr(action[1]))
                if action[2]:
                    qa.setStatusTip(self.tr(action[2]))
                self.connect(qa, SIGNAL("triggered()"), action[3])
                qa.setEnabled(action[4])
                menu.addAction(qa)
            else:
                menu.addSeparator()
        return menu

    def initialize_menu_bar(self):
        file_actions = ( 
                ("&New", "Ctrl+N", "New sprite.", self.new_map, True),
                ("&Open", "Ctrl+O", "Open.", self.load, True),
                ("SEPARATOR",None,None,None, True),
                ("&Save", "Ctrl+S", "Save sprite.", self.save, True),
                ("Save As...", None, "Save sprite as...", self.saveas, True),
                ("Save Flat File...", None, "Save flat map...", self.save_flat, True),
                ("SEPARATOR",None,None,None, True),
                ("Quit", "Ctrl+Q", "Quit", self.quit, True),
                )
        self.add_menu("&File", file_actions)

    def new_map(self):
        self.map.clear()
        self.maptilelist.initialize_list()

    def load(self):
        self.loadname = QFileDialog.getOpenFileName(self, "Load Sprite")
        if len(self.loadname) > 0:
            self.map.load(self.loadname)
            self.maptilelist.initialize_list()
            self.savename = self.loadname

    def save(self):
        """Save a Brick Sprite file, optionally first prompting for filename."""
        if len(self.savename) == 0:
            self.savename = QFileDialog.getSaveFileName(self, "Save Map As...")

        if self.savename:
            self.map.save(self.savename)

    def save_flat(self):
        """Save a flat map file."""
        self.savename = QFileDialog.getSaveFileName(self, "Save Flat Map As...")

        if self.savename:
            self.map.save_flat(self.savename)

    def saveas(self):
        """Save a Brick Sprite file, first prompting for filename."""
        self.savename = QFileDialog.getSaveFileName(self, "Save Map As...")

        if self.savename:
            self.map.save(self.savename)

    def quit(self):
        # TODO: Confirm/save dialog.
        sys.exit(0)
